package com.shzu.jwc.ty.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.shzu.jwc.ty.bean.PeTest;
import com.shzu.jwc.ty.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.InvigilatorInfo;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.service.InvigilatorInfoservice;
import com.shzu.jwc.ty.service.PeTestservice;
import com.shzu.jwc.ty.service.SystemYearservice;
import com.shzu.jwc.ty.service.UserTeacherInfoservice;

@Controller
@RequestMapping("petest")
public class PeTestcontroller {

	@Autowired
	PeTestservice peTestservice;
	
	@Autowired
	SystemYearservice systemYearservice;
	
	@Autowired
	UserTeacherInfoservice userTeacherInfoservice;
	
	@Autowired
	InvigilatorInfoservice invigilatorInfoservice;
	
	@RequestMapping("/petest.action")
	public String showpetest(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model)
	{
		Page page=new Page(peTestservice.countpetestpage(),pn);
		page.setPageSize(20);
		List<PeTest> data=peTestservice.getpetestpage(page);
		model.addAttribute("data", data);
		model.addAttribute("page",page);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "petest/petest";
	}
	
	@RequestMapping("/selectdate.action")
	public String selectdate(@RequestParam("systemYearId") Integer systemYearId,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model,HttpSession session)
	{
		if(systemYearId==0)
		{
			Page page=new Page(peTestservice.countpetestpage(),pn);
			page.setPageSize(20);
			List<PeTest> data=peTestservice.getpetestpage(page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		else
		{
			Page page=new Page(peTestservice.countpetestpage(),pn);
			page.setPageSize(20);
			List<PeTest> data=peTestservice.bydatesearch(systemYearId,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
			session.setAttribute("systemYearId", systemYearId);
		}
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "petest/petest";
	}
	
	@ResponseBody
	@RequestMapping("/insertpetest.action")
	public Msg insert(PeTest peTest)
	{
		try {
			peTestservice.insertpetest(peTest);
			return Msg.success("操作成功");
		}catch(Exception ex) {
			return Msg.error("操作失败,"+ex.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping("/deletepetest.action")
	public Msg deletepetest(@RequestParam("id")Integer peTestId)
	{
		try {
			peTestservice.delete(peTestId);
			return Msg.success("取消成功");
		}catch(Exception ex)
		{
			return Msg.error("取消失败,"+ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/copyone.action")
	public Msg copyone(@RequestParam("id")Integer peTestId){
		try{
			peTestservice.copyone(peTestId);
			return Msg.success("操作成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}
	}
	
	@RequestMapping("/update.action")
	public String update(@RequestParam("id")Integer peTestId,Model model)
	{
		model.addAttribute("data", peTestservice.getone(peTestId));
		return "petest/update";
	}
	
	@ResponseBody
	@RequestMapping("/updatefun.action")
	public Msg updatefun(PeTest peTest)
	{
		peTest.setPeTestState(0);
		try {
			peTestservice.update(peTest);
			return Msg.success("操作成功");
		}catch(Exception ex)
		{
			return Msg.error("操作失败,"+ex.getMessage());
		}
	}
	
	@RequestMapping("/searchpetest.action")
	public String searchpetest(@RequestParam(value="pn",defaultValue="1")Integer pn,@RequestParam("peTestName") String peTestName,Model model)
	{
		List<PeTest> data=peTestservice.search(peTestName);
		model.addAttribute("data", data);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "petest/petest";
	}
	
	@ResponseBody
	@RequestMapping("/searchteacher.action")
	public Msg searchteacher(@RequestParam("value") String userTeacherInfoName)
	{
		return Msg.success("操作成功").add("teacherdata", userTeacherInfoservice.serach(userTeacherInfoName));
	}
	
	@ResponseBody
	@RequestMapping("/addinvigilatorinfo.action")
	public Msg addinvigilatorinfo(InvigilatorInfo invigilatorInfo)
	{
		invigilatorInfo.setExamType("体能测试");
		Integer statue=invigilatorInfoservice.insert(invigilatorInfo);
		if(statue>0) {
			
			return Msg.success("插入成功").add("invigilatorInfo", invigilatorInfoservice.selectone(statue));
		}
		else if(statue==-1)
			return Msg.error("已安排过该老师");
		else
			return Msg.error("插入失败");
	}
	
	@ResponseBody
	@RequestMapping("/selectjk.action")
	public Msg selectjk(Integer examId)
	{
		return Msg.success("操作成功").add("data", invigilatorInfoservice.selectbyempleid(examId,"体能测试"));
	}
	
	@ResponseBody
	@RequestMapping("/deletejk.action")
	public Msg deletejk(Integer invigilatorInfoId)
	{
		try
		{
			invigilatorInfoservice.delete(invigilatorInfoId);
			return Msg.success("删除成功");
		}catch(Exception ex)
		{
			return Msg.error("删除失败"+ex.getMessage());
		}
	}
}
